package wstest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;

import utils.Utils;

public class WsServlet extends WebSocketServlet
{
 //   private final Set<SireWebSocket> _members = new CopyOnWriteArraySet<ChatWebSocket>();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws javax.servlet.ServletException ,IOException
    {
        getServletContext().getNamedDispatcher("default").forward(request,response);
    };

    @Override
    protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
    {
    	//System.out.println("PATH: "+getServletContext().getRealPath(arg0));
    	//System.out.println(request.getRemoteHost());
    	//System.out.println(request.getRemoteAddr());
    	InetAddress inetAddress;
    	String ipAddress = null;
		try {
			inetAddress = InetAddress.getByName(request.getRemoteAddr());
			//Get the host name  
	    	ipAddress = inetAddress.getHostName();  
	    	  
	    	//Print the host name 
	    	Utils.out("\n-- New connection --");
	    	Utils.out("+ Host: "+ipAddress);
	    	Utils.out("+ IP: "+request.getRemoteAddr());
	    	Utils.out("--------------------\n");
	    	//System.out.println(ipAddress); 
		} catch (UnknownHostException e) {
			Utils.out(e.getMessage());
		}  
    	  
    	 
        return new SireWebSocket(ipAddress);
    }
    
}